package me.wangcai.trafficpoliceai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.wangcai.trafficpoliceai.enums.PermissionEnum;
import me.wangcai.trafficpoliceai.mapper.UserMapper;
import me.wangcai.trafficpoliceai.model.ResponseInfo;
import me.wangcai.trafficpoliceai.model.entity.User;
import me.wangcai.trafficpoliceai.service.IUserService;
import me.wangcai.trafficpoliceai.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;

    public User login(String username, String password){
        return userMapper.login(username, Md5Util.md5(password));
    }

    public ResponseInfo getUserList(int page, int amount){
        HashMap<String,Object> map = new HashMap<>();
        map.put("count",userMapper.selectCount(null));
        map.put("list",userMapper.getUserList((page - 1) * amount,amount));
        return ResponseInfo.success("查询成功",map);
    }

    public ResponseInfo filterUser(Map<String,String> filterMap){
        QueryWrapper<User> wrapper = Wrappers.query();
        filterMap.forEach((k,v) -> {
            wrapper.like(k,v);
        });
        return ResponseInfo.success("查询成功",userMapper.selectList(wrapper));
    }

    public boolean editUser(int id, String password, String number, String mail, PermissionEnum permission){
        return userMapper.editUser(id,password,number,mail,permission);
    }
    public User getUserByName(String name){
        return userMapper.getUserByName(name);
    }
    public boolean register(User user){
        try{
            return userMapper.register(user);
        }catch (Exception err){
            return false;
        }
    }


    public User getUserById(int id){
        return userMapper.getUserById(id);
    }
}
